﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================

using DevExpress.XtraBars.ToolbarForm;
using System;
using System.Windows.Forms;
using XCI.Helper;
using XCI.Windows;

namespace XCI.Setting
{
    public partial class FrmSettingVGridForm : ToolbarForm
    {
        private VGridControlPlus vgridControl;

        public FrmSettingVGridForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (WinFormHelper.IsDesignMode()) WinFormHelper.InitDesignFont();
        }

        public void Init(VGridControlPlus gridControl)
        {
            this.vgridControl = gridControl;
            this.barId.EditValue = vgridControl.Id;
            InitControls();
            TabDelayLoad.Init(tabPane1, name =>
            {
                switch (name)
                {
                    case nameof(tabRows):
                        settingVGridRows1.Init(this.vgridControl);
                        break;
                    case nameof(tabAppearance):
                        gridAppearance1.Init(this.vgridControl.Appearance);
                        break;
                    case nameof(tabTemplate):
                        gridTemplate1.Init(this.vgridControl.SettingManager, "vgrid_:_");
                        break;
                    case nameof(tabAdvanced):
                        gridAdvanced1.SelectedObject = this.vgridControl;
                        break;
                }
            });
        }

        private void InitControls()
        {
            
        }

        private void Form_Load(object sender, EventArgs e)
        {

        }

        private async void btnReset_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要重置表格配置吗？重置后此表格的配置会全部删除") != DialogResult.Yes) return;

            await vgridControl.SettingManager.DeleteAllAsync();
            this.Close();
        }

        private async void btnSave_Click(object sender, EventArgs e)
        {
            await vgridControl.SettingManager.SaveCurrentAsync();
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private async void btnRestoreDefault_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要恢复表格默认配置吗？恢复会丢失当前配置") != DialogResult.Yes) return;

            await vgridControl.SettingManager.LoadDefaultAsync();
            MessageBoxHelper.ShowMessage("恢复表格默认配置成功,请点保存确认");
            this.Close();
        }

        private async void btnSaveDefault_Click(object sender, EventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要把当前配置设为默认配置吗？") != DialogResult.Yes) return;
            await vgridControl.SettingManager.SaveDefaultAsync();
            MessageBoxHelper.ShowMessage("默认配置设置成功");
        }
    }
}